import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Flight, Gate } from '../types/flight'
import { isTimeOverlap } from '../utils/timeUtils'

export const useGanttStore = defineStore('gantt', () => {
  // 状态
  const currentDate = ref(new Date())
  const flights = ref<Flight[]>([])
  const gates = ref<Gate[]>([])

  // 模拟数据生成
  const initMockData = () => {
    // 初始化机位数据
    gates.value = [
      { id: '103', name: '103(C)', type: 'domestic', visible: true },
      { id: '104', name: '104(C)', type: 'domestic', visible: true },
      { id: '105', name: '105(C)', type: 'domestic', visible: true },
      { id: '106', name: '106(C)', type: 'domestic', visible: true },
      { id: '107', name: '107(C)', type: 'domestic', visible: true },
      { id: '108', name: '108(D)', type: 'domestic', visible: true },
      { id: '109', name: '109(C)', type: 'domestic', visible: true },
      { id: '110', name: '110(C)', type: 'domestic', visible: true },
      { id: '200', name: '200(D)', type: 'domestic', visible: true },
      { id: '201', name: '201(E)', type: 'international', visible: true },
      { id: '202', name: '202(C)', type: 'domestic', visible: true },
      { id: '203', name: '203(C)', type: 'domestic', visible: true },
      { id: '204', name: '204(C)', type: 'domestic', visible: true },
      { id: '205', name: '205(C)', type: 'domestic', visible: true },
      { id: '206', name: '206(C)', type: 'domestic', visible: true },
      { id: '207', name: '207(C)', type: 'domestic', visible: true },
      { id: '208', name: '208(D)', type: 'domestic', visible: true },
      { id: '209', name: '209(E)', type: 'international', visible: true },
      { id: '210', name: '210(D)', type: 'domestic', visible: true },
      { id: '211', name: '211(D)', type: 'domestic', visible: true },
      { id: '212', name: '212(C)', type: 'domestic', visible: true },
      { id: '301', name: '301(F)', type: 'international', visible: true },
    ]

    // 初始化航班数据（基于你提供的图片）
    const today = new Date(currentDate.value)
    today.setHours(0, 0, 0, 0)

    flights.value = [
      // 103(C) - HU7639
      {
        id: 'f1',
        flightNumber: 'HU7639',
        gateId: '103',
        startTime: new Date(today.getTime() + 6 * 60 * 60 * 1000), // 06:00
        endTime: new Date(today.getTime() + 9 * 60 * 60 * 1000), // 09:00
        status: 'normal'
      },
      // 103(C) - 6011/
      {
        id: 'f2',
        flightNumber: '6011/',
        gateId: '103',
        startTime: new Date(today.getTime() + 9.5 * 60 * 60 * 1000), // 09:30
        endTime: new Date(today.getTime() + 11 * 60 * 60 * 1000), // 11:00
        status: 'normal'
      },
      // 103(C) - HU7640/HU7441
      {
        id: 'f3',
        flightNumber: 'HU7640/HU7441',
        gateId: '103',
        startTime: new Date(today.getTime() + 12 * 60 * 60 * 1000), // 12:00
        endTime: new Date(today.getTime() + 14 * 60 * 60 * 1000), // 14:00
        status: 'normal'
      },
      // 104(C) - HU7163
      {
        id: 'f4',
        flightNumber: 'HU7163',
        gateId: '104',
        startTime: new Date(today.getTime() + 6 * 60 * 60 * 1000), // 06:00
        endTime: new Date(today.getTime() + 8 * 60 * 60 * 1000), // 08:00
        status: 'normal'
      },
      // 104(C) - 4635
      {
        id: 'f5',
        flightNumber: '4635',
        gateId: '104',
        startTime: new Date(today.getTime() + 9 * 60 * 60 * 1000), // 09:00
        endTime: new Date(today.getTime() + 10 * 60 * 60 * 1000), // 10:00
        status: 'normal'
      },
      // 104(C) - 9131
      {
        id: 'f6',
        flightNumber: '9131',
        gateId: '104',
        startTime: new Date(today.getTime() + 10.5 * 60 * 60 * 1000), // 10:30
        endTime: new Date(today.getTime() + 11.5 * 60 * 60 * 1000), // 11:30
        status: 'normal'
      },
      // 104(C) - HU7164/H
      {
        id: 'f7',
        flightNumber: 'HU7164/H',
        gateId: '104',
        startTime: new Date(today.getTime() + 12 * 60 * 60 * 1000), // 12:00
        endTime: new Date(today.getTime() + 13.5 * 60 * 60 * 1000), // 13:30
        status: 'normal'
      },
      // 106(C) - HU7698
      {
        id: 'f8',
        flightNumber: 'HU7698',
        gateId: '106',
        startTime: new Date(today.getTime() + 6 * 60 * 60 * 1000), // 06:00
        endTime: new Date(today.getTime() + 8 * 60 * 60 * 1000), // 08:00
        status: 'normal'
      },
      // 106(C) - CA1991/CA19
      {
        id: 'f9',
        flightNumber: 'CA1991/CA19',
        gateId: '106',
        startTime: new Date(today.getTime() + 9.5 * 60 * 60 * 1000), // 09:30
        endTime: new Date(today.getTime() + 11 * 60 * 60 * 1000), // 11:00
        status: 'normal'
      },
      // 106(C) - SC8063/S
      {
        id: 'f10',
        flightNumber: 'SC8063/S',
        gateId: '106',
        startTime: new Date(today.getTime() + 12 * 60 * 60 * 1000), // 12:00
        endTime: new Date(today.getTime() + 13 * 60 * 60 * 1000), // 13:00
        status: 'normal'
      },
      // 108(D) - KY3005
      {
        id: 'f11',
        flightNumber: 'KY3005',
        gateId: '108',
        startTime: new Date(today.getTime() + 6 * 60 * 60 * 1000), // 06:00
        endTime: new Date(today.getTime() + 18 * 60 * 60 * 1000), // 18:00
        status: 'normal'
      },
      // 201(E) - MU9694
      {
        id: 'f12',
        flightNumber: 'MU9694',
        gateId: '201',
        startTime: new Date(today.getTime() + 6 * 60 * 60 * 1000), // 06:00
        endTime: new Date(today.getTime() + 7.5 * 60 * 60 * 1000), // 07:30
        status: 'normal'
      },
      // 201(E) - MU2144/ML
      {
        id: 'f13',
        flightNumber: 'MU2144/ML',
        gateId: '201',
        startTime: new Date(today.getTime() + 12 * 60 * 60 * 1000), // 12:00
        endTime: new Date(today.getTime() + 14 * 60 * 60 * 1000), // 14:00
        status: 'normal'
      },
      // 207(C) - GY7133/GY7134
      {
        id: 'f14',
        flightNumber: 'GY7133/GY7134',
        gateId: '207',
        startTime: new Date(today.getTime() + 11 * 60 * 60 * 1000), // 11:00
        endTime: new Date(today.getTime() + 22 * 60 * 60 * 1000), // 22:00
        status: 'normal'
      },
      // 210(D) - MU9915
      {
        id: 'f15',
        flightNumber: 'MU9915',
        gateId: '210',
        startTime: new Date(today.getTime() + 6 * 60 * 60 * 1000), // 06:00
        endTime: new Date(today.getTime() + 18 * 60 * 60 * 1000), // 18:00
        status: 'normal'
      },
      // 212(C) - MU2409/
      {
        id: 'f16',
        flightNumber: 'MU2409/',
        gateId: '212',
        startTime: new Date(today.getTime() + 12 * 60 * 60 * 1000), // 12:00
        endTime: new Date(today.getTime() + 20 * 60 * 60 * 1000), // 20:00
        status: 'normal'
      },
    ]
  }

  // 可见的机位
  const visibleGates = computed(() => gates.value.filter(g => g.visible))

  // 获取指定机位的航班
  const getFlightsByGate = (gateId: string) => {
    return flights.value.filter(f => f.gateId === gateId)
  }

  // 检查航班时间冲突
  const checkFlightConflict = (
    flightId: string,
    gateId: string,
    startTime: Date,
    endTime: Date
  ): boolean => {
    const gateFlights = getFlightsByGate(gateId)
    return gateFlights.some(f => {
      if (f.id === flightId) return false
      return isTimeOverlap(startTime, endTime, f.startTime, f.endTime)
    })
  }

  // 更新航班位置
  const updateFlightPosition = (
    flightId: string,
    gateId: string,
    startTime: Date
  ) => {
    const flight = flights.value.find(f => f.id === flightId)
    if (!flight) return

    const duration = flight.endTime.getTime() - flight.startTime.getTime()
    const endTime = new Date(startTime.getTime() + duration)

    const hasConflict = checkFlightConflict(flightId, gateId, startTime, endTime)

    flight.gateId = gateId
    flight.startTime = startTime
    flight.endTime = endTime
    flight.status = hasConflict ? 'warning' : 'normal'

    // 更新 zIndex：最近拖拽的航班在最上层
    // 找到当前最大的 zIndex
    const maxZIndex = Math.max(0, ...flights.value.map(f => f.zIndex || 0))
    flight.zIndex = maxZIndex + 1
  }

  // 切换机位可见性
  const toggleGateVisibility = (gateId: string) => {
    const gate = gates.value.find(g => g.id === gateId)
    if (gate) {
      gate.visible = !gate.visible
    }
  }

  // 批量隐藏机位
  const hideGates = (gateIds: string[]) => {
    gateIds.forEach(gateId => {
      const gate = gates.value.find(g => g.id === gateId)
      if (gate) {
        gate.visible = false
      }
    })
  }

  // 显示所有机位
  const showAllGates = () => {
    gates.value.forEach(g => g.visible = true)
  }

  // 更新当前日期
  const setCurrentDate = (date: Date) => {
    currentDate.value = date
  }

  return {
    currentDate,
    flights,
    gates,
    visibleGates,
    initMockData,
    getFlightsByGate,
    checkFlightConflict,
    updateFlightPosition,
    toggleGateVisibility,
    hideGates,
    showAllGates,
    setCurrentDate
  }
})
